package com.qcby.train.mybaitsplusdemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcby.train.mybaitsplusdemo.entity.Meet;
import com.qcby.train.mybaitsplusdemo.entity.Student;
import com.qcby.train.mybaitsplusdemo.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: Demo0513Controller
 * @description:
 * @author: lxt
 * @create: 2021-05-13 21:29
 **/
@Slf4j
@RestController
@RequestMapping("demo0513")
public class Demo0513Controller implements ApplicationContextAware {

    @Autowired
    private IStudentService studentService;


    @RequestMapping("listStudentPage")
    public IPage<Student> listStudentPage(Integer pageNo,int pageSize,Student student){
        log.info("pageNo:{}",pageNo);
        log.info("pageSize:{}",pageSize);
        Page<Student> page = new Page<Student>(pageNo,pageSize);

        IPage<Student>  pageInfo = studentService.listStudentPage(page,student);
        return pageInfo;
    }

    @GetMapping("demo3")
    @ResponseBody
    public List<Map> demo3(){
        List<Meet> meetList = new ArrayList<>();
        meetList.add(new Meet("2020-10-11","1"));
        meetList.add(new Meet("2020-10-11","1"));
        meetList.add(new Meet("2020-10-12","3"));
        meetList.add(new Meet("2020-10-12","3"));
        meetList.add(new Meet("2020-10-12","4"));
        // 对list转map
        Map<String, List<Meet>> groupByDate = meetList.stream().collect(Collectors.groupingBy(Meet::getDate));
        List<Map> list = new ArrayList<>();
        for (String s : groupByDate.keySet()) {
            Map<String,Object> mapT = new HashMap<>();
            mapT.put("meetDate",s);
            mapT.put("list",groupByDate.get(s));
            list.add(mapT);
        }
        return list;
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("controller-applicationContext:{}",applicationContext);
        applicationContext.getBean(Demo0521Controller.class);
    }
}
